import 'package:flutter/material.dart';
import 'package:flutter_soundmagus/page/delay/delay_controller.dart';
import 'package:flutter_soundmagus/util/util.dart';
import 'package:flutter_soundmagus/widget/eq/eq.dart';
import 'package:flutter_soundmagus/widget/page_base.dart';
import 'package:flutter_soundmagus/widget/widget.dart';
import 'package:get/get.dart';
import 'package:keyboard_actions/keyboard_actions.dart';

import '../../widget/menu_bottom_bar.dart';
import 'gain_item.dart';

///延时页面
class PageGain extends PageBase {
  const PageGain({Key? key}) : super(key: key);

  @override
  _PageGainState createState() => _PageGainState();
}

class _PageGainState extends PageBaseState<PageGain> {
  @override
  bool get menuHidden => false;

  @override
  void initState() {
    super.initState();

    Get.put(DelayController());
  }

  @override
  Widget buildBody(BuildContext context) {
    return DelayView();
  }
}

class DelayView extends GetView<DelayController> {
  DelayView({Key? key}) : super(key: key);
  //单位
  final units = UnitType.ms.names;

  final focusNodes = List.generate(kChCount, (index) => FocusNode());

  KeyboardActionsConfig _buildConfig(BuildContext context) {
    return KeyboardActionsConfig(
      keyboardActionsPlatform: KeyboardActionsPlatform.ALL,
      keyboardBarColor: Colors.grey[200],
      nextFocus: false,
      actions: [
        ...focusNodes.map((e) => KeyboardActionsItem(
              focusNode: e,
            )),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    print('build delayview');
    return Column(
      children: [
        Expanded(
          child: /*KeyboardActions(
              config: _buildConfig(context),
              child:*/
              Padding(
            padding: const EdgeInsets.only(top: 35, left: 15),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: List.generate(5, (index) {
                if (index == 2) {
                  //中间空隙 主音量
                  return _slider();
                }
                /**
                 * 0————0-1
                 * 1————2-3
                 * 2————4-5
                 */
                var ch = index * 2;
                if (index > 2) {
                  ch = (index - 1) * 2;
                }
                return Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    GainItem(
                      index: ch,
                      focusNode: focusNodes[0],
                    ),
                    GainItem(
                      index: ch + 1,
                      focusNode: focusNodes[0],
                    ),
                  ],
                );
              }),
            ),
          ),
        ),
        SizedBox(
          height: 5,
        ),
        _bottomNav(),
      ],
    );
  }

  /// 主音量
  Widget _slider() {
    return Column(
      children: [
        'g_vol'.uiimgw(42.w),
        Expanded(
          child: SizedBox(
            width: 40,
            child: Obx(() => VSlider(
                  value: controller.volume.value,
                  max: 0,
                  min: -20,
                  axis: Axis.vertical,
                  onChange: (v) => controller.volume.value = v,
                )),
          ),
        ),
        Padding(
          padding: const EdgeInsets.only(top: 0, bottom: 10),
          child: Obx(() => ZFButton(
                title: '${controller.volume.value.toInt()}dB',
                style: TextStyle(fontSize: 10),
              )),
        ),
        Obx(() => ZFButton(
              title: 'Mute',
              selected: controller.mute.value,
              onPressed: controller.mute.toggle,
            )),
      ],
    );
  }

  Widget _bottomNav() {
    return MenuBottomBar(title: 'Gain');
  }
}
